package com.wanpinghui.supplier.object;

import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.wanpinghui.supplier.R;
import com.wanpinghui.supplier.common.BaseActivity;
import com.wanpinghui.supplier.object.adapter.FeedBackConfirmAdapter;
import com.wanpinghui.supplier.widgets.CustomExpandableListView;
import com.wanpinghui.supplier.widgets.TitlePanelLayout;
import com.wanpinghui.wphlibrary.WPHSdk;
import com.wanpinghui.wphlibrary.model.object.ObjectService;
import com.wanpinghui.wphlibrary.model.object.bean.FeedBackReasonBean;
import com.wanpinghui.wphlibrary.model.object.bean.FeedbackInfoBean;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

import retrofit.callback.YRequestCallback;

/***
 * 功能描述:
 * 作者:qiujialiu
 * 时间:2017/12/28
 ***/
@ContentView(R.layout.layout_activity_feedback_confirm)
public class FeedBackConfirmActivity extends BaseActivity {
    @ViewInject(R.id.tpl_title)
    private TitlePanelLayout mLayoutTitle;
    @ViewInject(R.id.tv_feedback_type)
    private TextView textViewType;
    @ViewInject(R.id.tv_type_explain)
    private TextView textViewTypeExplain;
    @ViewInject(R.id.lv_reasons)
    private CustomExpandableListView listViewReasons;
    @ViewInject(R.id.tv_explain_tip)
    private TextView textViewExplainTip;
    @ViewInject(R.id.tv_explain_content)
    private TextView textViewExplainContent;

    private FeedBackConfirmAdapter confirmAdapter;

    private int type;
    private long successAt;//竞价成功时间
    private long demandId;
    private boolean isSend;//是否是派单

    @Override
    public String getName() {
        return null;
    }

    @Override
    public boolean isBar() {
        return false;
    }

    @Override
    public void initViews() {
        if (getIntent() != null) {
            demandId = getIntent().getLongExtra("id", 0);
            successAt = getIntent().getLongExtra("success_at", 0);
            type = getIntent().getIntExtra("type", 2);
            isSend = getIntent().getBooleanExtra("dispatch", false);
        }
        confirmAdapter = new FeedBackConfirmAdapter(this);
        listViewReasons.setAdapter(confirmAdapter);

        listViewReasons.setGroupIndicator(null);
    }

    @Override
    public void bindEvents() {
        mLayoutTitle.setOnClickBacktrack(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        confirmAdapter.setOnReasonClickListener(new FeedBackConfirmAdapter.OnReasonClickListener() {
            @Override
            public void onGroupClick(int position, int key) {
                if (type == 1) {
                    if (position == 0) {
                        feedback(0);
                    } else {
                        finish();
                    }
                } else {
                    if (position == 3) {
                        finish();
                    } else if (position == 2) {
                        feedback(key);
                    }
                }
            }

            @Override
            public void onChlidClick(int groupPostion, int childPosition, int key) {
                feedback(key);
            }
        });
    }

    private void feedback(int key) {
        WPHSdk.getService(ObjectService.class)
                .objectFeedback(demandId, key, type == 2 ? 0 : 1, new YRequestCallback<FeedbackInfoBean>() {
                    @Override
                    public void onSuccess(FeedbackInfoBean var1) {
                        if (type == 1) {
                            Intent intent = new Intent(FeedBackConfirmActivity.this, FeedbackResultActivity.class);
                            intent.putExtra("title", "锁定成功");
                            intent.putExtra("desception", "我们将不再派发给其它工程商，加油吧！");
                            intent.putExtra("id", demandId);
                            startActivity(intent);
                        } else {
                            Intent intent = new Intent(FeedBackConfirmActivity.this, FeedbackResultActivity.class);
                            intent.putExtra("title", "反馈成功！");
                            intent.putExtra("desception", "审核通过后，" + var1.getPunish_bid_point() + "汇币将退返至您的账户");
                            intent.putExtra("id", demandId);
                            startActivity(intent);
                        }
                    }

                    @Override
                    public void onFailed(int var1, String message) {
                        showWarningText(message);
                    }

                    @Override
                    public void onException(Throwable var1) {
                        var1.printStackTrace();
                        showWarningText("出错了[" + var1.toString() + "]");
                    }
                });
    }

    @Override
    public void initData() {
        if (type == 1) {
            List<FeedBackReasonBean> list = new ArrayList<>();
            int price = 0;
            if ((System.currentTimeMillis() / 1000) - successAt < (60 * 30)) {
                price = 100;
            } else if ((System.currentTimeMillis() / 1000) - successAt < (60 * 60 * 6)) {
                price = 50;
            } else if ((System.currentTimeMillis() / 1000) - successAt < (60 * 60 * 24)) {
                price = 10;
            }
            list.add(new FeedBackReasonBean("继续锁定，需支付" + price + "汇币"));
            list.add(new FeedBackReasonBean("我再考虑考虑"));
            textViewType.setText("需要独家锁定？");
            textViewTypeExplain.setText("独家锁定后，我们将不再派发给其它工程商。");
            textViewExplainTip.setText("规则说明");
            textViewExplainContent.setText(
                    "1、30分钟内锁定，需支付100汇币\n" +
                            "2、6小时内锁定，需支付50汇币\n" +
                            "3、24小时内锁定，需支付10汇币\n" +
                            "4、从抢单成功开始计时");
            confirmAdapter.setDatas(list);
        } else if (type == 2) {
            List<FeedBackReasonBean> list = new ArrayList<>();
            FeedBackReasonBean bean1 = new FeedBackReasonBean("订单信息无效，全款退返");
            List<FeedBackReasonBean.KeyValueData> data = new ArrayList<>();
            data.add(new FeedBackReasonBean.KeyValueData(1, "客户暂无需求"));
            data.add(new FeedBackReasonBean.KeyValueData(2, "多次尝试，均联系不上客户"));
            data.add(new FeedBackReasonBean.KeyValueData(5, "客户仅需报价，完全没有采购意向"));
            bean1.setDatas(data);
            list.add(bean1);
            FeedBackReasonBean bean2 = new FeedBackReasonBean("订单信息有误，全款退返");
            List<FeedBackReasonBean.KeyValueData> data2 = new ArrayList<>();
            data2.add(new FeedBackReasonBean.KeyValueData(6, "同行下单，但订单描述中未说明"));
            data2.add(new FeedBackReasonBean.KeyValueData(7, "客户实际预算与订单描述不符"));
            data2.add(new FeedBackReasonBean.KeyValueData(8, "订单描述严重有误"));
            bean2.setDatas(data2);
            list.add(bean2);
            if (isSend) {
                list.add(new FeedBackReasonBean(99, "无理由退款", false));
            } else if ((System.currentTimeMillis() / 1000) - successAt > (60 * 60 * 24)) {
                list.add(new FeedBackReasonBean(99, "无理由退款（已超过24小时）", false));
            } else {
                list.add(new FeedBackReasonBean(99, "无理由退款", true));
            }
            list.add(new FeedBackReasonBean("我再考虑考虑"));
            textViewType.setText("取消跟进吗？");
            textViewTypeExplain.setText("取消后，您将无法继续联系客户。");
            textViewExplainTip.setText("无理由退款规则说明");
            textViewExplainContent.setText(
                    "1、30分钟内取消，退返100%\n" +
                            "2、6小时内取消，退返50%\n" +
                            "3、24小时内取消，退返20%\n" +
                            "4、24小时后不再退返\n" +
                            "5、从抢单成功开始计时\n" +
                            "6、派单服务暂不支持无理由退款");
            confirmAdapter.setDatas(list);
        }

    }
}
